import math
def distancefile(lat,lon):
    lat=[]
    lon=[]
    dist=0
    for line in open("track","r"):
        line=line.split(";")
        lat.append(line[0])
        lon.append(line[1])     
    R=6371
    lat1,lon1=float(lat[0]),float(lon[0])
    for i in range(1,len(lat)):
        lat2,lon2=float(lat[i]),float(lon[i])
        dLat=math.radians(lat2-lat1)
        dLon=math.radians(lon2-lon1)
        a=math.sin(dLat/2)*math.sin(dLat/2)+math.cos(math.radians(lat1))*math.cos(math.radians(lat2))*math.sin(dLon/2)*math.sin(dLon/2)
        c=2*math.atan2(math.sqrt(a),math.sqrt(1-a)) 
        d=R*c
        dist=dist+d
        lat1,lon1=lat2,lon2
def distance(lat,lon):
    dist=0     
    R=6371
    lat1,lon1=float(lat[0]),float(lon[0])
    for i in range(1,len(lat)):
        lat2,lon2=float(lat[i]),float(lon[i])
        dLat=math.radians(lat2-lat1)
        dLon=math.radians(lon2-lon1)
        a=math.sin(dLat/2)*math.sin(dLat/2)+math.cos(math.radians(lat1))*math.cos(math.radians(lat2))*math.sin(dLon/2)*math.sin(dLon/2)
        c=2*math.atan2(math.sqrt(a),math.sqrt(1-a)) 
        d=R*c
        dist=dist+d
        lat1,lon1=lat2,lon2
    return dist

def displacement(source,latlon):
    R=6371
    nearest=[]
    lat1,lon1=float(source[0]),float(source[1])
    for i in latlon:
        lat2,lon2=float(i[0]),float(i[1])
        dLat=math.radians(lat2-lat1)
        dLon=math.radians(lon2-lon1)
        a=math.sin(dLat/2)*math.sin(dLat/2)+math.cos(math.radians(lat1))*math.cos(math.radians(lat2))*math.sin(dLon/2)*math.sin(dLon/2)
        c=2*math.atan2(math.sqrt(a),math.sqrt(1-a)) 
        d=R*c
        if d<5:
           nearest.append(latlon.index(i))
    return nearest

def initial(source,latlon):
    R=6371
    nearest=[]
    lat1,lon1=float(source[0]),float(source[1])
    for i in latlon:
        lat2,lon2=float(i[0]),float(i[1])
        dLat=math.radians(lat2-lat1)
        dLon=math.radians(lon2-lon1)
        a=math.sin(dLat/2)*math.sin(dLat/2)+math.cos(math.radians(lat1))*math.cos(math.radians(lat2))*math.sin(dLon/2)*math.sin(dLon/2)
        c=2*math.atan2(math.sqrt(a),math.sqrt(1-a)) 
        d=R*c
        nearest.append(d)
    return nearest             
